<template>
  <div>
    <!-- 搜索 -->
    <el-form :inline="true" :model="params" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button @click="toAddHandler">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.username" placeholder="请输入用户名" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.rolename" placeholder="请选择角色" clearable>
          <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="users.list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="roles[0].name" label="角色" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="status" label="状态" />
      <el-table-column prop="telephone" label="手机号" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <span @click="setHandler(scope.row)">设置</span>
          <span @click="deleteHandler(scope.row.id)">移除</span>
          <span>详情</span>
          <span @click="editHandler(scope.row)">修改</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :page-size="this.params.pageSize"
      layout="total, prev, pager, next"
      :total="users.total"
      @current-change="pageChangeHandler"
    />

    <!-- 添加模态框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      {{ form }}
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.realname" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.gender" label="male" value="male">男</el-radio>
          <el-radio v-model="form.gender" label="female" value="female">女</el-radio>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.telephone" />
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveHandler">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 设置模态框 -->
    <el-dialog title="设置角色" :visible.sync="setDialogVisible" width="50%" :before-close="handleClose">
      {{ setForm }}
      <el-form ref="setForm" :model="setForm" label-width="80px">
        <el-form-item label="用户名">
          {{ setUsername }}
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="setForm.roles" placeholder="请选择角色" clearable>
            <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSetHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs'
import { mapActions, mapState } from 'vuex'
export default {
  data() {
    return {
      users: [],
      params: {
        page: 1,
        pageSize: 5
      },
      dialogVisible: false,
      form: {},
      title: '',
      setDialogVisible: false,
      setForm: {},
      setUsername: '',
      user_row: {}
    }
  },
  computed: {
    ...mapState('role', ['roles'])
  },
  watch: {
    params: {
      handler: function() {
        this.queryFindUsers(this.params)
      },
      deep: true
    }
  },
  created() {
    this.queryFindUsers(this.params)
    this.findAllRoles()
  },
  methods: {
    ...mapActions('role', ['findAllRoles']),
    // 设置保存
    saveSetHandler() {
      this.setForm.id = this.user_row.id
      request.post('/baseUser/setRoles', qs.stringify(this.setForm)).then(res => {
        this.setDialogVisible = false
        this.queryFindUsers(this.params)
        this.$message({
          message: '更新成功',
          type: 'success'
        })
      })
    },
    // 设置
    setHandler(row) {
      this.setDialogVisible = true
      this.setUsername = row.username
      row.roles[0].roles = row.roles[0].id
      this.setForm = Object.assign({}, row.roles[0])
      // this.setForm.id = row.id
      this.user_row = row
    },
    // 删除
    deleteHandler(id) {
      request.get('/baseUser/deleteById', { params: { id }}).then(res => {
        console.log(res)
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
        this.queryFindUsers(this.params)
      })
    },
    // 修改
    editHandler(row) {
      this.dialogVisible = true
      this.form = row
      this.title = '修改用户'
    },
    // 保存
    saveHandler() {
      this.form.birth = this.form.birth.valueOf()
      request.post('/baseUser/saveOrUpdate', qs.stringify(this.form)).then(res => {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.dialogVisible = false
        this.queryFindUsers(this.params)
      })
    },
    // 添加
    toAddHandler() {
      this.dialogVisible = true
      this.title = '新增用户'
    },
    // 关闭添加模态框
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 分页
    pageChangeHandler(page) {
      this.params.page = page
    },
    // 分页查询
    queryFindUsers() {
      request.get('/baseUser/pageQuery', { params: this.params }).then(res => {
        this.users = res.data
      })
    }
  }
}
</script>
<style scoped>
    span {
        margin-right: .3rem;
        cursor: pointer;
    }
</style>

